﻿using System;
using AdAggregator.Website;
using RealEstateAggregator.Models.Parsers;
using RealEstateAggregator.Models.SearchData;
using RealEstateAggregator.Utils;
using SortingDirection = AdAggregator.SearchData.SortingDirection;

namespace RealEstateAggregator.Models.Websites
{
    public class DomiportaPlWebsite : BaseWebsite<MySearchData, DomiportaPlSearchData, MyAdvertisement, DomiportaPlParser>
    {
        public DomiportaPlWebsite(DomiportaPlParser parser) : base(parser)
        {
        }

        protected override DomiportaPlSearchData MapSearchData(MySearchData searchData)
        {
            var data = new DomiportaPlSearchData
            {
                AdvertisementType = MapAdType(searchData.AdvertisementType),
                City = searchData.City,
                EstateType = MapEstateType(searchData.EstateType),
                MarketType = searchData.MarketType,
                Keywords = searchData.Keywords,
                Localization = MapLocalization(searchData.City, searchData.Voivodship, searchData.District, searchData.Street),
                Page = searchData.Page,
                Voivodship = searchData.Voivodship,
                District = searchData.District,
                Street = searchData.Street,
                OnlyWithPhoto = searchData.OnlyWithPhoto,
                OrderBy = MapSortingOrder(searchData.OrderBy),
                SortingDirection = searchData.SortingDirection
            };
            if (searchData.AreaMin > 0) data.AreaMin = searchData.AreaMin.ToString();
            if (searchData.AreaMax > 0) data.AreaMax = searchData.AreaMax.ToString();
            if (searchData.RoomCountMin > 0) data.RoomCountMin = searchData.RoomCountMin.ToString();
            if (searchData.RoomCountMax > 0) data.RoomCountMax = searchData.RoomCountMax.ToString();
            if (searchData.PriceMin > 0) data.PriceMin = searchData.PriceMin.ToString();
            if (searchData.PriceMax > 0) data.PriceMax = searchData.PriceMax.ToString();
            if (searchData.PricePerMeterMin > 0) data.PricePerMeterMin = searchData.PricePerMeterMin.ToString();
            if (searchData.PricePerMeterMax > 0) data.PricePerMeterMax = searchData.PricePerMeterMax.ToString();
            if (searchData.PricePerMeterMin > 0) data.PricePerMeterMin = searchData.PricePerMeterMin.ToString();
            if (searchData.PricePerMeterMax > 0) data.PricePerMeterMax = searchData.PricePerMeterMax.ToString();
            if (searchData.YearBuiltMin > 0) data.YearBuiltMin = searchData.YearBuiltMin.ToString();
            if (searchData.YearBuiltMax > 0) data.YearBuiltMax = searchData.YearBuiltMax.ToString();
            if (searchData.FloorMin.HasValue) data.FloorMin = searchData.FloorMin.Value.ToString();
            if (searchData.FloorMax.HasValue) data.FloorMax = searchData.FloorMax.Value.ToString();
            return data;
        }

        private string MapSortingOrder(string orderBy)
        {
            switch (orderBy)
            {
                case "Price":
                case "PricePerMeter":
                    return "Price";
                case "Area":
                    return "Surface";
                default:
                    return String.Empty;
            }
        }

        private string MapLocalization(string city, string voivodship, string district, string street)
        {
            var localization = String.Empty;
            if (!String.IsNullOrEmpty(city))
                localization += city;
            if (!String.IsNullOrEmpty(voivodship))
            {
                if (!String.IsNullOrEmpty(localization))
                    localization += ", ";
                localization += voivodship;
            }
            if (!String.IsNullOrEmpty(district))
            {
                if (!String.IsNullOrEmpty(localization))
                    localization += ", ";
                localization += district;
            }
            if (!String.IsNullOrEmpty(street))
            {
                if (!String.IsNullOrEmpty(localization))
                    localization += ", ";
                localization += street;
            }
            return localization;
        }

        private string MapEstateType(EstateType type)
        {
            switch (type)
            {
                case EstateType.House:
                    return "dom";
                case EstateType.Flat:
                    return "mieszkanie";
                case EstateType.Room:
                    return "pokoj";
                case EstateType.Commercial:
                    return "lokal_uzytkowy";
                case EstateType.Garage:
                    return "garaz";
                case EstateType.Land:
                    return "dzialke";
                default:
                    return "nieruchomosci";
            }
        }

        private string MapAdType(AdvertisementType type)
        {
            switch (type)
            {
                case AdvertisementType.Sale:
                    return "sprzedam";
                case AdvertisementType.Rent:
                    return "wynajme";
                default:
                    return "wszystkie";
            }
        }
    }
}